package com.mohan;

import com.mohan.bean.Red;
import com.mohan.config.MainConfigAutoWrite;
import com.mohan.config.MainConfigOfDataSource;
import com.mysql.cj.jdbc.MysqlConnectionPoolDataSource;
import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

import javax.sql.DataSource;

public class TestAutoWrite {
    @Test
    public void test01() {
        ApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigAutoWrite.class);
        for (String definitionName : applicationContext.getBeanDefinitionNames()) {
            System.out.println("definitionName = " + definitionName);
        }
        System.out.println(applicationContext.getBean("boss"));
    }


    @Test
    public void test02() {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(MainConfigOfDataSource.class);
        final String[] namesForType = applicationContext.getBeanNamesForType(DataSource.class);
        for (String name : namesForType) {
            System.out.println("name = " + name);
        }
        applicationContext.close();
    }

    /**
     *
     */
    @Test
    public void test03() {
        // 无参构造
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
        // 调用环境注入为prod环境
        applicationContext.getEnvironment().setActiveProfiles("prod");
        // 将配置源给注入
        applicationContext.register(MainConfigOfDataSource.class);
        // 启动刷新容器
        applicationContext.refresh();

        final String[] namesForType = applicationContext.getBeanNamesForType(DataSource.class);
        for (String name : namesForType) {
            System.out.println(name);
        }

        applicationContext.close();
    }
}
